package com.example.liuan.scananim;

import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private ImageView ivAccCpu1;
    private ImageView ivAccCpu2;

    boolean isOpen;
    private ClipDrawable drawableIvAccCpu2;
    private ClipDrawable drawableIvAccCpu1;
    int animDuration = 5000;
    private final int MSG_STARTANIM_OPEN = 1024;
    private final int MSG_STARTANIM_CLOSE = 1022;
    int progress = 0;
    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_STARTANIM_OPEN:
                    if (progress > 100) {
                        removeMessages(MSG_STARTANIM_OPEN);
                    }
                    progress += 1;
                    showImagePergress(progress, true);
                    sendEmptyMessageDelayed(MSG_STARTANIM_OPEN, animDuration / 100);
                    break;
                case MSG_STARTANIM_CLOSE:
                    if (progress > 100) {
                        removeMessages(MSG_STARTANIM_CLOSE);
                    }
                    progress += 1;
                    showImagePergress(progress, false);
                    sendEmptyMessageDelayed(MSG_STARTANIM_CLOSE, animDuration / 100);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivAccCpu1 = findViewById(R.id.iv_acc_cpu1);
        ivAccCpu2 = findViewById(R.id.iv_acc_cpu2);
        //获取图片所显示的ClipDrawble对象
        //从10000 到 1  到这 着显示
        drawableIvAccCpu2 = (ClipDrawable) ivAccCpu2.getDrawable();
        //从1 到10000   正着显示
        drawableIvAccCpu1 = (ClipDrawable) ivAccCpu1.getDrawable();
        showImageFirstPic(true);
        RelativeLayout relativeLayout = findViewById(R.id.rl_acp_acc);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(null);
                if (isOpen) {
                    close();
                } else {
                    openAnim();
                }


                isOpen = !isOpen;
            }
        });

    }

    //开的时候从上到下
    private void openAnim() {
        progress = 0;
        handler.sendEmptyMessage(MSG_STARTANIM_OPEN);
    }

    //关的时候从下到上
    private void close() {
        progress = 0;
        handler.sendEmptyMessage(MSG_STARTANIM_CLOSE);
    }


    /**
     * 从0-100
     */
    private void showImagePergress(int progress, boolean isFormTop) {
        //Log.e(TAG, "showImagePergress: progress" + progress);
        int num = (int) (10000 * progress / 100);
        drawableIvAccCpu2.setLevel(isFormTop ? num : 10000 - num);
        drawableIvAccCpu1.setLevel(isFormTop ? 10000 - num : num);
    }


    private void showImageFirstPic(boolean isShowFirst) {
        drawableIvAccCpu2.setLevel(isShowFirst ? 0 : 10000);
        drawableIvAccCpu1.setLevel(isShowFirst ? 10000 : 0);
    }


}
